package com.min.redis.common.exception;

import com.min.redis.common.enums.base.ErrorCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

/**
 * 通用的业务错误
 *
 * @author LuJ
 * @since 2023-10-26 14:48
 */
@Getter
@AllArgsConstructor
public class BizException extends RuntimeException {

    private static final long serialVersionUID = -671636769059697064L;
    /**
     * 错误码
     */
    private final Integer code;

    /**
     * 异常信息
     */
    private final String errorMsg;

    public BizException(ErrorCodeEnum errorCodeEnum) {
        super(errorCodeEnum.getMsg());
        this.code = errorCodeEnum.getCode();
        this.errorMsg = errorCodeEnum.getMsg();
    }

    public BizException(ErrorCodeEnum errorCodeEnum, String message) {
        super(errorCodeEnum.getMsg());
        this.code = errorCodeEnum.getCode();
        this.errorMsg = StringUtils.isBlank(message) ? errorCodeEnum.getMsg() : message;
    }
}
